package org.springblade.mach.service.impl;

import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.api.R;
import org.springblade.mach.entity.DrawingAttachApproval;
import org.springblade.mach.mapper.DrawingAttachApprovalMapper;
import org.springblade.mach.service.IDrawingAttachApprovalService;
import org.springblade.modules.resource.entity.Attach;
import org.springblade.modules.resource.service.IAttachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DrawingAttachApprovalServiceImpl extends BaseServiceImpl<DrawingAttachApprovalMapper, DrawingAttachApproval>
	implements IDrawingAttachApprovalService {
	@Autowired
	private IAttachService attachService;

	@Override
	@Transactional
	public Boolean handleDrawingAttachApproval(DrawingAttachApproval drawingAttachApproval) {
		Attach attach = attachService.getById(drawingAttachApproval.getAttachId());
		if (attach != null) {
			drawingAttachApproval.setOriginalName(attach.getOriginalName());
			drawingAttachApproval.setType(attach.getType());
		}
		if (drawingAttachApproval == null) {
			throw new ServiceException("参数不能为空!");
		}
		DrawingAttachApproval attachApproval = this.getDrawingAttachApproval(drawingAttachApproval);
		if (attachApproval != null) {
			drawingAttachApproval.setId(attachApproval.getId());
			this.updateById(drawingAttachApproval);
		} else {
			this.save(drawingAttachApproval);
		}
		return true;
	}

	@Override
	public DrawingAttachApproval getDrawingAttachApproval(DrawingAttachApproval drawingAttachApproval) {
		if (drawingAttachApproval == null) {
			throw new ServiceException("参数不能为空!");
		}
		return baseMapper.getDrawingAttachApproval(drawingAttachApproval);
	}

	@Override
	public List<DrawingAttachApproval> findHistoryApprovalList(Long drawingUploadId, String taskDefinitionKey) {
		if (drawingUploadId == null) {
			throw new ServiceException("图纸上传主键不能为空!");
		}
		return baseMapper.findHistoryApprovalList(drawingUploadId, taskDefinitionKey);
	}

	/**
	 * 复选同意审批
	 * @param drawingAttachApprovalList 图纸信息列
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public R approvalDrawingAttachApprovalByBatch(List<DrawingAttachApproval> drawingAttachApprovalList) {
		boolean result = false;
		drawingAttachApprovalList.forEach(drawingAttachApproval -> {
			Attach attach = attachService.getById(drawingAttachApproval.getAttachId());
			if (null != attach) {
				drawingAttachApproval.setOriginalName(attach.getOriginalName());
				drawingAttachApproval.setType(attach.getType());
			}
		});
		result = this.saveBatch(drawingAttachApprovalList);
		return R.data(result);
	}
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean logicDelApproval(Long uploadId, String taskDefinitionKey){
		return baseMapper.logicDel(uploadId, taskDefinitionKey) > 0 ? true : false;
	}
}
